package com.biz.primus.ms.sms.service.checker;

import com.biz.primus.model.misc.enums.ApiExceptionType;
import com.biz.primus.model.misc.exception.ApiException;
import com.biz.primus.model.misc.vo.ChannelBasedAuth;
import com.biz.primus.model.misc.vo.SendRequestVo;
import com.biz.primus.ms.sms.service.internal.SensitiveWordService;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yanweijin
 * @date 2017/5/31
 */
@Component
public class SensitiveWordChecker extends SendCheckerAdapter {

	@Autowired
	private SensitiveWordService sensitiveWordService;

	@Override
	public void check(ChannelBasedAuth cba) throws ApiException {
		if(cba instanceof SendRequestVo){
			String msg = ((SendRequestVo) cba).getFullText();
			Optional<String> sensitiveWord = sensitiveWordService.findSensitiveWord(msg);
			if (sensitiveWord.isPresent()) {
				throw new ApiException("敏感词:"+sensitiveWord.get(), ApiExceptionType.SENSITIVE_WORD);
			}
		}

	}

	@Override
	public int getOrder() {
		return super.getOrder();
	}
}
